/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.SanPhamCoTaiCuaHangDAO;
import com.miracleteam.miracle.entities.Sanphamcotaicuahang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DangKhoa
 */
public class SanPhamCoTaiCuaHangDAOImpl implements  SanPhamCoTaiCuaHangDAO{
Logger logger= Logger.getLogger(SanPhamCoTaiCuaHangDAOImpl.class);
    public List LayDSLoaiSPCuaCuaHang(String MaCH) {
           List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "select sp from Sanphamcotaicuahang sp where sp.daXoa = 0 and sp.gianhang.maGianHang=:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", MaCH);
            query.setFirstResult(0);
            query.setMaxResults(20);
            ds = query.list();

        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        return ds;
    }
        public Sanphamcotaicuahang LaySPCoTaiCuaHang(String ma) {
        Sanphamcotaicuahang sp = null;

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sp from Sanphamcotaicuahang sp where sp.daXoa=0 AND sp.maSanPhamCoTaiCuaHang=:maSP";
            Query query = session.createQuery(hql);
            query.setString("maSP", ma);   
            sp = (Sanphamcotaicuahang)query.uniqueResult(); 
        } catch (HibernateException e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        } finally {
            session.close();
        }
        return sp;
    }
          public  boolean capNhat(Sanphamcotaicuahang sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (sp == null) {
            return false;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(sp);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
        } finally {
            session.close();
        }
        return true;
    }
              public String PhatSinhKhoaChinh()
    {
        String kq="SP-GH-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
      public  boolean Them(Sanphamcotaicuahang sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (sp == null) {
            return false;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(sp);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
        } finally {
            session.close();
        }
        return true;
    }
    
}
